﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.IO;
using System.Net;

namespace IGGCore
{
    public class TransactionParser
    {
        public List<TransactionDay> GetTransactions()
        {
            List<TransactionDay> transactionDays = new List<TransactionDay>();

            string url = "http://www.cbssports.com/mlb/transactions";
            string transContent = "";
            StreamReader reader = null;
            try
            {
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                reader = new StreamReader(request.GetResponse().GetResponseStream());
                transContent = reader.ReadToEnd();
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }

            transContent = transContent.Substring(transContent.IndexOf("<div id=\"pageRow\">"));
            transContent = transContent.Substring(transContent.IndexOf("<table class=\"data\""));
            transContent = transContent.Substring(0, transContent.IndexOf("</table>"));
            transContent = transContent.Substring(transContent.IndexOf("<tr class=\"subtitle\">"));
            string[] days = transContent.Split(new string[]{"<tr class=\"subtitle\"><td colspan=\"3\">"}, StringSplitOptions.RemoveEmptyEntries);

            foreach (string day in days)
            {
                TransactionDay transactionDay = new TransactionDay();
                transactionDay.Date = DateTime.Parse(day.Substring(0, day.IndexOf("</td>")));
                string content = day.Substring(day.IndexOf("</tr>"));
                content = content.Substring(content.IndexOf("</tr>", 4) + 5);
                string[] singleTransStr = content.Split(new string[] { "</tr>" }, StringSplitOptions.RemoveEmptyEntries);
                foreach (string singleTrans in singleTransStr)
                {
                    Transaction transaction = new Transaction();
                    transaction.Date = transactionDay.Date;

                    string teamstr = singleTrans.Substring(0, singleTrans.IndexOf("</a>"));
                    transaction.Team = teamstr.Substring(teamstr.LastIndexOf(">")+1);

                    string playerstr = singleTrans.Substring(singleTrans.IndexOf("</a>"));
                    transaction.Player = playerstr.Substring(playerstr.IndexOf("left\">") + 6);
                    transaction.Player = transaction.Player.Substring(0, transaction.Player.IndexOf("</td>"));
                    if (transaction.Player.IndexOf("</a>") > -1)
                    {
                        transaction.Player = transaction.Player.Substring(0, transaction.Player.IndexOf("</a>"));
                        transaction.Player = transaction.Player.Substring(transaction.Player.LastIndexOf(">")+1);
                    }

                    string actionstr = singleTrans.Substring(singleTrans.LastIndexOf("left\">"));
                    transaction.Action = actionstr.Substring(actionstr.IndexOf("left\">") + 6);
                    transaction.Action = transaction.Action.Substring(0, transaction.Action.IndexOf("</td>"));

                    transaction.Action = transaction.Action.Replace("( ", "(");

                    transaction.ActionAbbr = transaction.Action.Replace("(", "");
                    transaction.ActionAbbr = transaction.ActionAbbr.Replace(")", ".");
                    transaction.ActionAbbr = transaction.ActionAbbr.Replace(", ", ",");
                    transaction.ActionAbbr = transaction.ActionAbbr.Replace(",", "");
                    transaction.ActionAbbr = transaction.ActionAbbr.Replace("Minor", "Min.");
                    transaction.ActionAbbr = transaction.ActionAbbr.Replace("Min.s", "minors.");
                    transaction.Action = transaction.Action.Replace(")(", ") (");

                    transactionDay.Transactions.Add(transaction);
                }
                transactionDays.Add(transactionDay);
            }
            return transactionDays;
        }

    }
}
